
import re

f=open(r"D:\7z\a.txt","r",encoding='utf-8')
x=f.read()
f.close()
r=re.findall("v\[([a-z_]+)\]",x)

print(r)

# 读取文件内容
with open(r"D:\7z\a.txt", 'r', encoding='utf-8') as file:
    content = file.read()

# 定义正则表达式模式
pattern = r"\[''(.*?)''\]"

# 使用 re.sub 进行替换，将匹配的内容修改为 ['字符串']
modified_content = re.sub(pattern, r"['\1']", content)

# 将修改后的内容写回文件
with open(r"D:\7z\a.txt", 'w', encoding='utf-8') as file:
    file.write(modified_content)

'''
#我现在想在一个文件中，把所有的[字符串]改为['字符串']，其中字符串是任意字符，需要加一对单引号
import re
with open(r'D:\7z\a.txt', 'r', encoding='utf-8') as file:
    content = file.read()
pattern = r'\[(.*?)\]'
modified_content = re.sub(pattern, r"['\1']", content)
with open(r'D:\7z\a.txt', 'w', encoding='utf-8') as file:
    file.write(modified_content)

'''